﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.Orders;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.Order;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.Order
{
    public class PrePayOrderSendDeliveryRepository : SqlServerRespository<PrePayOrderDeliverySendEntity>,
        IPrePayOrderSendInfoRepository

    {
        public PrePayOrderSendDeliveryRepository()
        {
            Context = new WechatMallContext();
        }

        public PrePayOrderDeliverySendEntity GetOrderSendDeliveryInfo(string sno)
        {
            var sql =
                $@"SELECT  ddlsh AS OrderSno,
                ddjfsj AS DeliveryTime,
                ddjwd AS Location,
                ddcscode AS Adcode,
                dddbdz AS GpsAddress,
                ddshdz AS DetailAddress,
                ddshr AS Receiver,
                ddshrxb AS Sex,
                ddshrdh AS ReceiverTel,
                ddbz AS ReceiverDesc
                FROM    weiscdzfddjfxxpst                
                where ddlsh=@sno";
            return GetList(sql, new {sno}).FirstOrDefault();
        }

        public IList<PrePayOrderDeliverySendEntity> GetOrderSendDeliveryInfos(IList<string> snolist)
        {
            var condition = snolist.Aggregate(string.Empty, (current, sno) => current + sno + ",");
            if (condition.EndsWith(","))
                condition = condition.Left(condition.Length - 1);

            var sql =
                $@"SELECT  ddlsh AS OrderSno,
                ddjfsj AS DeliveryTime,
                ddjwd AS Location,
                ddcscode AS Adcode,
                dddbdz AS GpsAddress,
                ddshdz AS DetailAddress,
                ddshr AS Receiver,
                ddshrxb AS Sex,
                ddshrdh AS ReceiverTel,
                ddbz AS ReceiverDesc
                FROM    weiscdzfddjfxxpst   a                
                where ddlsh in ('{condition}')";
            return GetList(sql).ToList();
        }


        public void SaveOrderSendDelivery(PrePayOrderDeliverySendEntity entity, OrderMainInfoDto orderMain,
            UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from weiscdzfddjfxxpst where ddlsh=@sno", Context, unit,
                new {sno = orderMain.Sno});
            Add(entity, unit);
        }

        public void RemoveBatch(string sno, UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from weiscdzfddjfxxpst where ddlsh=@sno", Context, unit,
                new {sno});
        }
    }
}